<?php
declare (strict_types=1);

namespace app\tool\controller;

use think\db\exception\DataNotFoundException;
use think\Request;

class ArrController
{
    /*
     * num 随机几个数
     * data 一维数组
     * type 1 可以重复 0不重复
     * */
    static public function arr_random($num, $data, $type = 1)
    {
        if (!is_array($data)) {
            return ['code' => 0, 'msg' => '非数组'];
        }
        if (self::array_depth($data) != 1) {
            return ['code' => 0, 'msg' => '请输入一维数组'];
        }
        if ($type == 1) {
            $res = [];
            for ($i = 0; $i < $num; $i++) {
                $random = mt_rand(0, count($data) - 1);
                $res[$i] = $data[$random];
            }
            return ['code' => 1, 'msg' => '获取成功', 'data' => $res];
        } else {
            $count = count($data);
            if ($num > $count) {
                return ['code' => 0, 'msg' => '所需元素数量超过数组长度'];
            }
            $res = [];
            $selected = [];
            for ($i = 0; $i < $num; $i++) {
                do {
                    $random = mt_rand(0, $count - 1);
                } while (in_array($random, $selected)); // 确保不选择重复的元素
                $selected[] = $random;
                $res[] = $data[$random];
            }
            return ['code' => 1, 'msg' => '获取成功', 'data' => $res];
        }
    }

    static function array_depth($array)
    {
        $max_depth = 1;
        foreach ($array as $value) {
            if (is_array($value)) {
                $depth = array_depth($value) + 1;
                if ($depth > $max_depth) {
                    $max_depth = $depth;
                }
            }
        }
        return $max_depth;
    }

    static function inversion($arr)
    {
        if (is_array($arr)) {
            return array_reverse($arr);
        }
        if (is_string($arr)) {
            return self::mbStrrev($arr, $encoding = 'UTF-8');
        }
    }

    static function mbStrrev($str, $encoding)
    {
        $len = mb_strlen($str, $encoding);
        $reversed = '';
        for ($i = $len - 1; $i >= 0; $i--) {
            $reversed .= mb_substr($str, $i, 1, $encoding);
        }
        return $reversed;
    }

    static function findByKey($array, $key, $value)
    {
        $filtered = array_filter($array, function ($item) use ($key, $value) {
            return $item[$key] == $value;
        });
        return $filtered;
    }

    /*
     * 将什么替换成什么
     * */
    static function changeStr($from, $to, $str)
    {
        return str_replace($from, $to, $str);
    }

  /*  static function arrAdd($data, $point = '', $v)
    {
        if ($point == "end") {
            return array_push($data, $v);
        }
        if ($point == "start") {
            return array_unshift($data, $v);
        }

    }*/

    static function arrDel($data, $point = '')
    {

        if ($point == "end") {
            return array_pop($data);
        }
        if ($point == "start") {
            return array_shift($data);
        }
    }

    static function arrDelByKey($data, $k)
    {
        unset($data[$k]);
        return $data;
    }

    /*
     * 数组转字符串
     * $arr = ['Hello','World']
     * $res = implode("",$arr);
     * Hello World
     *
     * */
    static function arrToStr($arr, $word): string
    {
        return implode($word, $arr);
    }

    /*
     * 字符串分割成数组
     * */
    static function strToArr($k, $str): array
    {
        return explode($k, $str);
    }

    static function combined($arr, $value): array
    {
        return array_merge($arr, $value);
    }

    static function findKeyTrue($arr, $key): bool
    {
        return array_key_exists($key, $arr);
    }

}
